package org.wkg.lesson11;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class Demo_1103 {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(10);
        System.out.println("各就各位,预备...");

        for (int i = 0; i < 10; i++) {
            Thread player = new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "开跑.");
                try {
                    TimeUnit.SECONDS.sleep(2);
                    System.out.println(Thread.currentThread().getName() + "到达.");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                countDownLatch.countDown();
            },  "player" + i);
            player.start();
        }

        TimeUnit.MILLISECONDS.sleep(10);
        System.out.println("比赛进行中....");
        countDownLatch.await();//阻塞等待，一直等待countDownLatch变成0
        System.out.println("开始颁奖");
    }
}
